<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全能智慧助手 - 权益明账通</title>
    <style>
        /* 完全复制首页的样式 */
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f7fa;
            color: #333;
        }

        /* 导航菜单 - 与首页完全一致 */
        .main-nav {
            background-color: #1976d2;
            padding: 1rem;
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .main-nav ul {
            display: flex;
            list-style: none;
            margin: 0;
            padding: 0;
            justify-content: center;
        }

        .main-nav li {
            margin: 0 1rem;
        }

        .main-nav a {
            color: white;
            text-decoration: none;
            font-weight: 500;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            transition: background-color 0.3s;
        }

        .main-nav a:hover {
            background-color: rgba(255, 255, 255, 0.2);
        }

        .main-nav .active a {
            background-color: rgba(255, 255, 255, 0.3);
        }

        /* 介绍区域 - 与首页完全一致 */
        .intro {
            text-align: center;
            padding: 4rem 1rem;
            background: linear-gradient(135deg, #1976d2, #2196f3);
            color: white;
            /*margin-top: 2rem;*/
        }

        .intro h1 {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            text-shadow: 1px 1px 3px rgba(0,0,0,0.2);
        }

        .intro p {
            font-size: 1.2rem;
            max-width: 800px;
            margin: 0 auto;
            line-height: 1.6;
        }

        /* 知识板块标题 - 与首页完全一致 */
        .section-title {
            text-align: center;
            padding: 3rem 1rem 1rem;
            background-color: #f5f7fa;
        }

        .section-title h2 {
            font-size: 2.2rem;
            color: #1976d2;
            margin-bottom: 0.5rem;
            position: relative;
            display: inline-block;
        }

        .section-title h2::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 80px;
            height: 3px;
            background: linear-gradient(90deg, #1976d2, #2196f3);
            border-radius: 2px;
        }

        .section-title .subtitle {
            font-size: 1.1rem;
            color: #666;
            max-width: 600px;
            margin: 0 auto;
        }

        /* AI对话区域 - 修复输入框超出边界问题 */
        .ai-container {
            max-width: 800px;
            margin: 0 auto;
            padding: 0 2rem 2rem;
            background-color: #f5f7fa;
        }

        .ai-chat-box {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            margin-top: 2rem;
        }

        .chat-header {
            background: #1976d2;
            color: white;
            padding: 1.5rem;
            text-align: center;
        }

        .chat-header h3 {
            margin: 0 0 0.5rem 0;
            font-size: 1.5rem;
        }

        .chat-header p {
            margin: 0;
            opacity: 0.9;
            font-size: 1rem;
        }

        .chat-messages {
            height: 300px;
            overflow-y: auto;
            padding: 1rem;
            background: #fafafa;
            border-bottom: 1px solid #eee;
        }

        .message {
            margin-bottom: 1rem;
            padding: 0.75rem 1rem;
            border-radius: 8px;
            max-width: 80%;
            word-wrap: break-word;
        }

        .user-message {
            background: #e3f2fd;
            margin-left: auto;
            border-radius: 18px 18px 0 18px;
        }

        .ai-message {
            background: white;
            margin-right: auto;
            border-radius: 18px 18px 18px 0;
            border: 1px solid #eee;
            position: relative;
        }

        /* 修复输入框区域 - 确保不超出边界 */
        .chat-input-area {
            padding: 1rem;
            background: white;
        }

        .chat-input-area textarea {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #ddd;
            border-radius: 8px;
            resize: none;
            min-height: 80px;
            font-family: inherit;
            font-size: 1rem;
            box-sizing: border-box; /* 重要：确保padding和border包含在宽度内 */
            margin-bottom: 1rem;
        }

        .chat-input-area textarea:focus {
            outline: none;
            border-color: #1976d2;
            box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.1);
        }

        .chat-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .btn {
            padding: 0.6rem 1.2rem;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.9rem;
            font-weight: 500;
            transition: all 0.3s;
        }

        .btn-primary {
            background: #1976d2;
            color: white;
        }

        .btn-primary:hover {
            background: #1565c0;
            transform: translateY(-1px);
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #545b62;
        }

        .copy-btn {
            position: absolute;
            right: 8px;
            top: 8px;
            background: rgba(25, 118, 210, 0.1);
            border: 1px solid rgba(25, 118, 210, 0.3);
            border-radius: 4px;
            padding: 0.2rem 0.5rem;
            cursor: pointer;
            font-size: 0.7rem;
            color: #1976d2;
        }

        .copy-btn:hover {
            background: rgba(25, 118, 210, 0.2);
        }

        .loading {
            text-align: center;
            color: #666;
            padding: 2rem;
            font-style: italic;
        }

        .initial-prompt {
            text-align: center;
            color: #666;
            padding: 3rem 1rem;
            font-style: italic;
        }

        /* 移动端适配 */
        @media (max-width: 768px) {
            .main-nav ul {
                flex-wrap: wrap;
            }
            
            .main-nav li {
                margin: 0.5rem;
            }
            
            .ai-container {
                padding: 0 1rem 1rem;
                margin: 0 auto;
            }
            
            .chat-messages {
                height: 250px;
            }
            
            .message {
                max-width: 90%;
            }
            
            .chat-actions {
                flex-direction: column;
                gap: 0.5rem;
            }
            
            .btn {
                width: 100%;
            }
        }

        @media (max-width: 480px) {
            .intro h1 {
                font-size: 2rem;
            }
            
            .section-title h2 {
                font-size: 1.8rem;
            }
            
            .chat-header h3 {
                font-size: 1.3rem;
            }
        }
    </style>
</head>
<body>
    <!-- 导航菜单 - 与首页完全一致 -->
    <nav class="main-nav">
        <ul>
            <li><a href="./index.html">首页</a></li>
            <li><a href="./calculator.html">智能计算</a></li>
            <li><a href="./volunteer.html">志愿者平台</a></li>
            <li class="active"><a href="./AIassistant.html">全能智慧助手</a></li>
        </ul>
    </nav>

    <!-- 介绍区域 - 与首页完全一致 -->
    <section class="intro">
        <h1>全能智慧助手</h1>
        <p>智能AI助手为您提供消费维权咨询和问题解答服务</p>
    </section>

    <!-- AI对话板块标题 - 与首页风格一致 -->
    <div class="section-title">
        <h2>AI智能对话</h2>
        <p class="subtitle">请输入您的问题，获取专业解答</p>
    </div>

    <!-- AI对话区域 - 修复输入框超出边界问题 -->
    <div class="ai-container">
        <div class="ai-chat-box">
            <div class="chat-header">
                <h3>AI智能对话</h3>
                <p>请输入您的问题，获取专业解答（暂不支持连续对话）</p>
            </div>
            
            <div class="chat-messages" id="chatMessages">
                <div class="initial-prompt">对话准备就绪，请开始提问...</div>
            </div>
            
            <div class="chat-input-area">
                <textarea id="questionInput" placeholder="请输入您的问题..."></textarea>
                <div class="chat-actions">
                    <button class="btn btn-secondary" onclick="clearConversation()">清空对话</button>
                    <button class="btn btn-primary" onclick="sendQuestion()">发送问题</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        // API配置
        const AI_API = {
            chat: '/api/ai/chat'
        };
        
        let conversationHistory = [];
        
        // 发送问题到AI
        async function sendQuestion() {
            const input = document.getElementById('questionInput');
            const question = input.value.trim();
            
            if (!question) {
                alert('请输入问题');
                return;
            }
            
            // 添加用户消息
            addMessage('user', question);
            input.value = '';
            
            // 显示加载状态
            showLoading();
            
            try {
                const response = await fetch(AI_API.chat, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'text/plain'
                    },
                    body: question
                });
                
                let answer;
                const contentType = response.headers.get('content-type');
                
                if (contentType && contentType.includes('application/json')) {
                    const errorData = await response.json();
                    answer = errorData.error || '请联系网站管理员';
                } else {
                    answer = await response.text();
                }
                
                // 添加AI回答
                addMessage('ai', answer);
                
                // 保存对话历史
                conversationHistory.push(
                    { role: 'user', content: question },
                    { role: 'ai', content: answer }
                );
                
            } catch (error) {
                addMessage('ai', '网络错误，请稍后再试');
            }
        }
        
        // 添加消息到对话界面
        function addMessage(role, content) {
            const messagesDiv = document.getElementById('chatMessages');
            
            // 移除初始提示和加载状态
            const initialPrompt = messagesDiv.querySelector('.initial-prompt');
            const loading = messagesDiv.querySelector('.loading');
            if (initialPrompt) initialPrompt.remove();
            if (loading) loading.remove();
            
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${role}-message`;
            
            if (role === 'ai') {
                messageDiv.innerHTML = `
                    <div>${content}</div>
                    <button class="copy-btn" onclick="copyToClipboard(this)">复制</button>
                `;
            } else {
                messageDiv.textContent = content;
            }
            
            messagesDiv.appendChild(messageDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        }
        
        // 显示加载状态
        function showLoading() {
            const messagesDiv = document.getElementById('chatMessages');
            const loadingDiv = document.createElement('div');
            loadingDiv.className = 'loading';
            loadingDiv.textContent = '正在分析问题，请稍后...';
            messagesDiv.appendChild(loadingDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        }
        
        // 清空对话
        function clearConversation() {
            if (confirm('确定要清空当前对话吗？')) {
                const messagesDiv = document.getElementById('chatMessages');
                messagesDiv.innerHTML = '<div class="initial-prompt">对话准备就绪，请开始提问...</div>';
                conversationHistory = [];
            }
        }
        
        // 复制文本到剪贴板
        function copyToClipboard(button) {
            const message = button.parentNode.querySelector('div').textContent;
            navigator.clipboard.writeText(message).then(() => {
                const originalText = button.textContent;
                button.textContent = '已复制';
                setTimeout(() => {
                    button.textContent = originalText;
                }, 2000);
            });
        }
        
        // 回车键发送（Ctrl+Enter）
        document.getElementById('questionInput').addEventListener('keydown', function(e) {
            if (e.key === 'Enter' && e.ctrlKey) {
                e.preventDefault();
                sendQuestion();
            }
        });
    </script>
    <!-- 页脚版权信息 -->
    <footer style="text-align: center; padding: 2rem; background-color: #f0f0f0; color: #666; font-size: 0.9rem;">
        <p>曹惠宁 杜梓沫 杨雨昔 刘晓雨 宋金乔 张墨涵 李雨桐 作品 © 2025</p>
        <p>黑河学院经济管理学院、文化旅游学院、计算机与信息工程学院 软件项目开发社团 版权所有</p>
    </footer>
</body>
</html>